package com.fishery.search.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fishery.entity.Result;
import com.fishery.search.entity.Diagnose;
import com.fishery.search.entity.SearchCondition;
import com.fishery.search.service.DiagnoseSearch;
import com.fishery.search.service.DiagnoseService;
import com.fishery.search.util.SearchUtil;
import com.fishery.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ghost
 * @description 疾病诊断前端控制器
 * @since 2021-1-27
 */
@RestController
@CrossOrigin
@RequestMapping("/search/diagnose")
@Api(tags = "疾病诊断模块")
public class DiagnoseController {

	@Autowired
	private DiagnoseService diagnoseService;

	@Autowired
	private DiagnoseSearch diagnoseSearch;

	@PostMapping("/picture")
	@ApiOperation("图片上传接口")
	public Result uploadPicture(MultipartFile file) {
		String ret = FileUtil.upload(file);
		return Result.success().data(ret);
	}

	@PostMapping("/publish")
	@ApiOperation("发表疾病诊断文章")
	public Result publishArticle(@RequestBody Diagnose diagnose) {
		if (diagnose == null
				|| StringUtils.isEmpty(diagnose.getUserId())
				|| StringUtils.isEmpty(diagnose.getDiseaseName())) {
			return Result.error("发表失败");
		}
		boolean ret = diagnoseService.saveArticle(diagnose);
		return ret ? Result.success("发表成功") : Result.error("发表失败");
	}

	@GetMapping("/my/{userId}/{size}/{current}}")
	@ApiOperation("查询自己已发布的文章（不带高亮，带分页）")
	public Result searchMyArticles(@PathVariable(name = "userId") @ApiParam(required = true, value = "用户ID") String userId,
								   @PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
								   @PathVariable(name = "current") @ApiParam(required = true, value = "页码") int current) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查询失败");
		}
		Page<Diagnose> page = diagnoseService.getDiseaseDiagnoseInfo(userId, current, size);
		return Result.success().data(page);
	}

	@DeleteMapping("/{id}")
	@ApiOperation("删除疾病诊断文章")
	public Result deleteArticle(@PathVariable(name = "id") @ApiParam(required = true, value = "id") String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error("删除失败");
		}
		boolean ret = diagnoseService.deleteArticle(id);
		return ret ? Result.success("删除成功") : Result.error("删除失败");
	}

	@PostMapping
	@ApiOperation("分页高亮搜索疾病")
	public Result getInfo(@RequestBody @ApiParam(value = "搜索条件") SearchCondition searchCondition) {
		if (searchCondition == null
				|| StringUtils.isEmpty(searchCondition.getKeyword())
				|| StringUtils.isEmpty(searchCondition.getContent())) {
			return Result.error("搜索失败");
		}
		return Result.success().data(diagnoseSearch.searchInfo(searchCondition, null));
	}


	@PostMapping("/{timeSort}/{readSort}")
	@ApiOperation("分页带排序高亮搜索疾病")
	public Result searchInfo(@RequestBody @ApiParam(value = "搜索条件") SearchCondition searchCondition,
							 @PathVariable(name = "timeSort") @ApiParam(required = true, value = "按时间排序(填写0为默认、填写1升序、填写2降序)") int timeSort,
							 @PathVariable(name = "readSort") @ApiParam(required = true, value = "按阅读量排序(填写0为默认、填写1升序、填写2降序)") int readSort) {
		if (searchCondition == null
				|| StringUtils.isEmpty(searchCondition.getKeyword())
				|| StringUtils.isEmpty(searchCondition.getContent())) {
			return Result.error("搜索失败");
		}
		Map<String, SortOrder> sortOrderMap = new HashMap<>(2);
		SearchUtil.fillSortMap(sortOrderMap, "gmt_create.keyword", timeSort);
		SearchUtil.fillSortMap(sortOrderMap, "read_amount", readSort);
		return Result.success().data(diagnoseSearch.searchInfo(searchCondition, sortOrderMap));
	}

	@GetMapping("/{articleId}/{userId}")
	@ApiOperation("查看疾病文章")
	public Result searchInfoById(@PathVariable(name = "articleId") @ApiParam(value = "文章ID", required = true) String articleId,
								 @PathVariable(name = "userId") @ApiParam(value = "用户ID", required = true) String userId) {
		if (StringUtils.isEmpty(articleId)
				|| StringUtils.isEmpty(userId)) {
			return Result.error("查看失败");
		}
		return Result.success().data(diagnoseService.getArticleById(articleId, userId));
	}

	@PutMapping
	@ApiOperation("修改自己的文章")
	public Result updateMyArticle(@RequestBody Diagnose diagnose) {
		if (StringUtils.isEmpty(diagnose.getId()) || StringUtils.isEmpty(diagnose.getUserId())) {
			return Result.error("修改失败");
		}
		return diagnoseService.updateArticle(diagnose) ? Result.success("修改成功") : Result.error("修改失败");
	}

	@PutMapping("/update/readAmount")
	@ApiOperation("更新阅读量(供定时器模块调用)")
	public Result updateReadAmountOfArticle() {
		return diagnoseService.updateReadAmountOfArticleToDB() ? Result.success() : Result.error();
	}

}
